package edu.uct.tamnd.viettourism.citylist;

import java.io.IOException;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import edu.uct.tamnd.viettourism.R;
import edu.uct.tamnd.viettourism.catlist.CatList;
import edu.uct.tamnd.viettourism.helper.DatabaseHelper;

public class CityList extends ListActivity {

	private DatabaseHelper dbOpenHelper ;
		
	private ArrayList<CityItem> cityItems;
	private CityItemArrayAdapter adapter;
	DatabaseHelper mHelper;
	
	Bundle bundle = new Bundle();

    private static final String TAG = "CityList";

    
    @Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.city_list);
		setTitle("Danh sách thành phố");
		openDatabase();
				
		cityItems = new ArrayList<CityItem>();
		cityItems = DatabaseHelper.readCity();
		adapter = new CityItemArrayAdapter(CityList.this, R.layout.city_row_item, cityItems);
		setListAdapter(adapter);
		
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
		Intent intent = new Intent(CityList.this, CatList.class);
		
		bundle.putInt("cityID", cityItems.get(position).getId());
		intent.putExtras(bundle);
		Log.d(TAG, "Gui cityID = "+cityItems.get(position).getId());	
		
		bundle.putString("cityName", cityItems.get(position).getName());
		intent.putExtras(bundle);
		Log.d(TAG, "Gui cityName = "+cityItems.get(position).getName());
		
		startActivity(intent);
	}
	
//	private ArrayList<CityItem> generalExampleData(){
//		ArrayList<CityItem> listCity= new ArrayList<CityItem>();
//		
//		listCity.add(new CityItem("Hà Nội","Thủ đô 1000 năm văn hiến","hanoi"));
//		listCity.add(new CityItem("Huế","Nét đẹp dịu dàng pha lẫn trầm tư","hue"));
//		listCity.add(new CityItem("Đà Nẵng","Thành phố bên bờ biển xanh","danang"));
//		listCity.add(new CityItem("Thành phố Hồ Chí Minh","Thành phố năng động, nhộn nhịp","hcm"));
//		
//		return listCity;
//	}
	
	private void openDatabase(){
		dbOpenHelper = new DatabaseHelper(this);
		try {
			dbOpenHelper.createDatabase();
			Log.d(TAG, "Đã gọi hàm create");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.d("CityList", e.getMessage());
			Toast.makeText(this, "Lỗi db", Toast.LENGTH_LONG).show();
		}
	}
	
}
